package marchsoft.modules.tiku.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import marchsoft.annotation.rest.AnonymousGetMapping;
import marchsoft.annotation.rest.AnonymousPostMapping;
import marchsoft.base.PageVO;
import marchsoft.modules.tiku.entity.ESafeCase;
import marchsoft.modules.tiku.entity.EStudyData;
import marchsoft.modules.tiku.entity.dto.ESafeCaseNameDto;
import marchsoft.modules.tiku.entity.dto.ESafeCaseSearchDto;
import marchsoft.modules.tiku.entity.dto.EStudyDataTag;
import marchsoft.modules.tiku.mapper.ESafeCaseMapper;
import marchsoft.response.Result;
import org.springframework.cache.annotation.CacheConfig;

import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import marchsoft.modules.tiku.service.IESafeCaseService;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;


/**
* <p>
* 安全案例 前端控制器
* </p>
* @author zsy
* @since 2022-03-29
*/
@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "安全案例模块")
@RequestMapping("/api/eSafeCase")
public class ESafeCaseController {
    private final IESafeCaseService eSafeCaseService;

    @AnonymousGetMapping(value = "front")
    public Result<Object> getFrontById(Long id){
        ESafeCase e =eSafeCaseService.getById(id);
        return Result.success(e);
    }
    @AnonymousGetMapping(value = "/front-all")
    public Result<Object> getFrontAll(PageVO pageVO){
        IPage<ESafeCase> safeCaseIPage=eSafeCaseService.page(pageVO.buildPage());
        return Result.success(safeCaseIPage);
    }
    @AnonymousPostMapping(value = "/front/like")
    public void updateLike(@RequestBody EStudyDataTag studyDataTag){
        ESafeCase eSafeCase = new ESafeCase();
        eSafeCase.setId(studyDataTag.getId());
        eSafeCase.setTags(studyDataTag.getCount());
        eSafeCaseService.updateById(eSafeCase);
    }
    @AnonymousPostMapping(value = "/front/dislike")
    public void updateDisLike(@RequestBody EStudyDataTag studyDataTag){
        ESafeCase eSafeCase = new ESafeCase();
        eSafeCase.setId(studyDataTag.getId());
        eSafeCase.setDislike(studyDataTag.getCount());
        eSafeCaseService.updateById(eSafeCase);
    }
    @AnonymousPostMapping(value = "/front/read")
    public void getFrontRead(@RequestBody EStudyDataTag eStudyDataTag){
        ESafeCase eSafeCase=new ESafeCase();
        eSafeCase.setId(eStudyDataTag.getId());
        eSafeCase.setPageView(eStudyDataTag.getCount());
        eSafeCaseService.updateById(eSafeCase);
//        ESafeCase eSafeCase=eSafeCaseService.getById(id);
    }

    @GetMapping
    public Result<Object> getAllCase(PageVO pageVO, ESafeCaseSearchDto searchDto) {
        IPage<ESafeCaseNameDto> eSafeCases = eSafeCaseService.queryAllCase(pageVO,searchDto);
        System.out.printf("222222222222222", eSafeCases);
        return Result.success(eSafeCases);
    }
    @ApiOperation("分页条件获取分类数据")
    @RequestMapping("/pid")
    @AnonymousGetMapping
    public Result<Object> getPidCase(@RequestBody Set<Long> ids) {
        ESafeCase eSafeCase = new ESafeCase();
        for (Long id : ids) {
            eSafeCase = eSafeCaseService.queryPidCase(id);
        }
        return Result.success(eSafeCase);
    }
    @PostMapping
    public Result<Object> addESafeCase(@RequestBody ESafeCase eSafeCase) {
        eSafeCaseService.addESafeCase(eSafeCase);
        System.out.printf("123456", eSafeCase.getId());
        return Result.success();
    }

    @DeleteMapping
    public Result<Object> deleteCase(@RequestBody Set<Long> ids) {
        Set<ESafeCase> menuSet = new HashSet<>();
        for (Long id : ids) {
            eSafeCaseService.deleteById(id);
        }
        return Result.success();
    }

    @PutMapping
    public Result<Object> updateESafeType(@RequestBody  ESafeCase eSafeCase) {
        eSafeCaseService.updateESafeCase(eSafeCase);
        return Result.success();
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response, ESafeCaseSearchDto searchDto) throws Exception {
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(-1);
        pageVO.setSize(-1);
        eSafeCaseService.download(eSafeCaseService.queryAllCase(pageVO, searchDto), response);
    }

}
